package com.mycompany.myapp.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : swagger2配置
 * @Author 黄志成
 * @Date 2019-08-05
 * @Version
 */

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket createRestApiWeb() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("demo/web")
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.mycompany.myapp.web.rest"))
            .paths(PathSelectors.regex("/api/.*"))
            .build()
            .securitySchemes(securitySchemes())
            .securityContexts(securityContexts());
    }

/*    @Bean
    public Docket createRestApiWX() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("ktv/wx")
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.winsour.ktv.web.rest.controller.wx"))
            .paths(PathSelectors.regex("/ktv/wx/.*"))
            .build()
            .securitySchemes(securitySchemes())
            .securityContexts(securityContexts());
    }*/

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("XXX项目api文档")
            .description("XXX项目api文档")
            .termsOfServiceUrl("")
            .version("0.0.1")
            .build();
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("Authorization", "authorization", "header"));
        return apiKeys;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
            .securityReferences(defaultAuth())
            .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

}
